package com.abso.sunlight.explorer;

import java.util.Collections;
import java.util.Map;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.NotEnabledException;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.handlers.IHandlerService;

/**
 * Miscellaneous command utilities.
 */
public class CommandUtils {

    /** Variable name identifying the current legislator editor. */
    public static final String LEGISLATOR_EDITOR_VAR = "legislatorEditor";

    /**
     * Executes a command.
     * 
     * @param cmdId
     *            the command's identifier.
     * @param shell
     *            the shell.
     * @return the result of the command's execution.
     */
    public static Object executeCommand(String cmdId, Shell shell) {
        return executeCommand(cmdId, null, shell);
    }

    /**
     * Executes a command.
     * 
     * @param cmdId
     *            the command's identifier.
     * @param contextParams   additional context parameters.
     * @param shell
     *            the shell.
     * @return the result of the command's execution.
     */
    public static Object executeCommand(String cmdId, Map<String, Object> contextParams, Shell shell) {
        try {
            IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
            ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
            Command command = commandService.getCommand(cmdId);
            IEvaluationContext context = handlerService.getCurrentState();
            if (contextParams != null) {
                for (Map.Entry<String, Object> entry : contextParams.entrySet()) {
                    context.addVariable(entry.getKey(), entry.getValue());
                }
            }
            ExecutionEvent event = new ExecutionEvent(command, Collections.EMPTY_MAP, null, context);
            return command.executeWithChecks(event);
        } catch (ExecutionException e) {
            CongressExplorerPlugin.getDefault().showErrorDialog(shell, "Error", "Unable to perform a new search", e.getMessage(), e);
        } catch (NotDefinedException e) {
            CongressExplorerPlugin.getDefault().showErrorDialog(shell, "Error", "Unable to perform a new search", e.getMessage(), e);
        } catch (NotEnabledException e) {
            CongressExplorerPlugin.getDefault().showErrorDialog(shell, "Error", "Unable to perform a new search", e.getMessage(), e);
        } catch (NotHandledException e) {
            CongressExplorerPlugin.getDefault().showErrorDialog(shell, "Error", "Unable to perform a new search", e.getMessage(), e);
        }
        return null;
    }

}
